package cc.fine.svc.main.controller.tbl;


import cc.fine.svc.common.utils.R;
import cc.fine.svc.main.bean.FineMessage;
import cc.fine.svc.main.bean.vo.CommonPublishVo;
import cc.fine.svc.main.bean.vo.CommonViewVo;
import cc.fine.svc.main.service.tbl.TblCommonService;
import cc.fine.svc.main.utils.MqUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wyihe
 * @since 2023-10-25
 */
@RestController
@RequestMapping("common")
@Api(description = "用户评论相关接口")
public class TblCommonController {

    @Autowired
    private TblCommonService commonService;

    @Autowired
    private MqUtils mqUtils;

    @ApiOperation("用户给发表评论(pig:为空——是视频下的评论，为评论id——是评论下的评论")
    @PostMapping("/common/")
    public R common(@RequestBody(required = false) CommonPublishVo vo) {

        if (StringUtils.isEmpty(vo.getPid())) {
            vo.setPid("root");
        }

        // 异步给视频发表评论
        mqUtils.send(new FineMessage(FineMessage.PUBLISH_COMMON, vo));

        return R.ok();
    }

    @ApiOperation("获取视频评论列表, page从0开始。返回参数中，root为分页的父级评论，child为父级评论下的所有子评论")
    @GetMapping("/commons/{videoId}")
    public R commons(@PathVariable String videoId,
                     Integer page, Integer limit) {
        List<CommonViewVo> res = commonService.commons(videoId, page, limit);
        return R.ok().data("res", res);
    }

}

